package cn.lyl.org.controller;

import cn.lyl.basic.annotation.PreAuthorize;
import cn.lyl.org.domain.Department;
import cn.lyl.org.domain.Employee;
import cn.lyl.org.query.EmployeeQuery;
import cn.lyl.org.service.IEmployeeService;
import cn.lyl.basic.util.AjaxResult;
import cn.lyl.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口:部门
 */
@Api(value = "部门接口类",description = "处理部门的CRUD操作")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查找一个
     * @return
     */
        //前端：get http://localhost:8080/employee/1
    @ApiOperation(value = "查找单个对象")
    @GetMapping("/{id}")
    @PreAuthorize(name = "员工查找单个对象",sn = "employee:getOne") //value后面用来做权限校验
    public Employee findOne(@PathVariable("id")Long id){
          return employeeService.findOne(id);
    }

    /**
     * 查找所有
     * @return
     */
    ////前端：get http://localhost:8080/employee
    @ApiOperation(value = "查找所有")
    @GetMapping
    @PreAuthorize(name = "员工查找所有",sn = "employee:getAll") //value后面用来做权限校验
    public List<Employee> findAll(){
           return employeeService.findAll();
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/employee/1
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工删除",sn = "employee:delete") //value后面用来做权限校验
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改
     * @param employee
     * @return
     */
    //前端：put http://localhost:8080/employee
    @ApiOperation(value = "添加或修改")
    @PutMapping
    @PreAuthorize(name = "添加员工",sn = "employee:save") //value后面用来做权限校验
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.add(employee);
            }else{
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询或高级查询
     * @param employeeQuery
     * @return
     */
    ////前端：post http://localhost:8080/employee json对象{}
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    @PreAuthorize(name = "员工列表",sn = "employee:list") //value后面用来做权限校验
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
           return employeeService.queryPage(employeeQuery);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(name = "员工批量删除",sn = "employee:patchDel") //value后面用来做权限校验
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 查询指定店铺的所有管理人员
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable("shopId") Long shopId){

        return employeeService.queryByShopId(shopId);
    }

}
